/**
 * 
 */
package ua.ws.feedsreader.actions;

import java.util.Map;

import org.apache.struts2.convention.annotation.Result;
import org.openrdf.query.MalformedQueryException;
import org.openrdf.query.QueryEvaluationException;
import org.openrdf.repository.RepositoryException;

import ua.ws.feedsreader.common.FeedEntry;
import ua.ws.feedsreader.common.User;
import ua.ws.feedsreader.db.SemanticManager;
import ua.ws.feedsreader.utils.Log;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @author ricardo
 *
 */
@Result(location="filterfeeds.jsp",name="success")
public class FilterFeeds extends ActionSupport {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 8231705564284998511L;
	private String keywords;
	private Integer search;
	private Map<Integer, FeedEntry> categoryFeeds;
	
	
	@Override
	public String execute() {
		
		Map<String, Object> session = ActionContext.getContext().getSession();
		Log.debug("ChooseCategories");
		User user = (User) session.get("USER");
		Log.debug("User:"+user.getLogin() );
		SemanticManager semanticManager = new SemanticManager();

		Log.debug( keywords );
		
		try {
			if( search == 1 ) {
				categoryFeeds = semanticManager.search( keywords );				
			} else {
				categoryFeeds = semanticManager.getCategoryFeeds( keywords );
			}
		} catch (RepositoryException | MalformedQueryException | QueryEvaluationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return ERROR;
		}
		
		return SUCCESS;
	}

	/**
	 * @return the keywords
	 */
	public String getKeywords() {
		return keywords;
	}

	/**
	 * @param keywords the keywords to set
	 */
	public void setKeywords(String keywords) {
		this.keywords = keywords;
	}

	/**
	 * @return the categoryFeeds
	 */
	public Map<Integer, FeedEntry> getCategoryFeeds() {
		return categoryFeeds;
	}

	/**
	 * @param categoryFeeds the categoryFeeds to set
	 */
	public void setCategoryFeeds(Map<Integer, FeedEntry> categoryFeeds) {
		this.categoryFeeds = categoryFeeds;
	}

	/**
	 * @return the search
	 */
	public Integer getSearch() {
		return search;
	}

	/**
	 * @param search the search to set
	 */
	public void setSearch(Integer search) {
		this.search = search;
	}

}
